package logic;

import java.io.File;

public class FileHelper {
	//założenie, że extension to ostatni segment nazwy przy separacji kropkami
	public static String getExtension(File file) {
		String name = file.getName();
		System.out.println("last" + name.lastIndexOf('.'));
		int lastDotIndex;
		if ((lastDotIndex = name.lastIndexOf('.')) == -1) {
			return "";
		}
		return name.substring(lastDotIndex+1);
	}
	
	public static String buildPath(String... elements) {
		StringBuilder sb = new StringBuilder();
		sb.append(elements[0]);
		for (int i = 1; i < elements.length; i++) {
			sb.append('/').append(elements[i]);
		}
		return sb.toString();
	}
	
	//metoda w obecnej formie działa tylko dla ścieżek lokalnych
	public static boolean isDir(String path) {
		File file = new File(path);
		return file.isDirectory();
	}

	public static Protocol getProtocol(String path) throws UnsupportedProtocol {
		if (path.startsWith("/") || path.startsWith("file:///") || path.startsWith("./") || path.matches("[A-Za-z]:.*")) { 
			return Protocol.FILE_URI;
		}
		throw new UnsupportedProtocol(path);
	}
	
}
